package com.micangcaihang.micang.adapter;

import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.micangcaihang.common.utils.ConvertUtils;
import com.micangcaihang.micang.R;
import com.micangcaihang.micang.entity.TaskPriceSurveyEntity;

import java.util.List;

/**
 * <pre>
 *     author : Fandy
 *     e-mail : fandayongfly@gmail.com
 *     time   : 2017/07/25
 *     update : 2017/07/25
 *     desc   : 中介两个月快速成交价适配器
 *     version: 1.0
 * </pre>
 */

public class DAgentPriceAdapter extends BaseQuickAdapter<TaskPriceSurveyEntity.ZhongJie, BaseViewHolder> {

    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param layoutResId The layout resource id of each item.
     * @param data        A new list is created out of this one to avoid mutable list
     */
    public DAgentPriceAdapter(@LayoutRes int layoutResId, @Nullable List<TaskPriceSurveyEntity.ZhongJie> data) {
        super(layoutResId, data);
    }


    /**
     * Implement this method and use the helper to adapt the view to the given item.
     *
     * @param helper A fully initialized helper.
     * @param item   The item that needs to be displayed.
     */
    @Override
    protected void convert(BaseViewHolder helper, TaskPriceSurveyEntity.ZhongJie item) {
        int layoutPosition = helper.getLayoutPosition();
        helper.addOnClickListener(R.id.iv_delete);
        EditText et_unit_price = helper.getView(R.id.et_unit_price);
        EditText et_reference_name = helper.getView(R.id.et_reference_name);
        EditText et_evaluate_source = helper.getView(R.id.et_evaluate_source);
        EditText et_contacts = helper.getView(R.id.et_contacts);
        et_unit_price.setText(item.getUnit_price());
        et_reference_name.setText(item.getReference_name());
        et_evaluate_source.setText(item.getEvaluate_source());
        et_contacts.setText(item.getContacts());
        MTextWatcher mTextWatcher1 = new MTextWatcher(1, item);
        MTextWatcher mTextWatcher2 = new MTextWatcher(2, item);
        MTextWatcher mTextWatcher3 = new MTextWatcher(3, item);
        MTextWatcher mTextWatcher4 = new MTextWatcher(4, item);
        et_unit_price.addTextChangedListener(mTextWatcher1);
        et_reference_name.addTextChangedListener(mTextWatcher2);
        et_evaluate_source.addTextChangedListener(mTextWatcher3);
        et_contacts.addTextChangedListener(mTextWatcher4);

        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        int dp_22 = ConvertUtils.dp2px(15);
        params.setMargins(0, 0, 0, dp_22);
        helper.itemView.setLayoutParams(params);
    }

    public class MTextWatcher implements TextWatcher {
        private int type;
        private TaskPriceSurveyEntity.ZhongJie item;

        public MTextWatcher(int type, TaskPriceSurveyEntity.ZhongJie item) {
            this.type = type;
            this.item = item;
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            switch (type) {
                case 1:
                    item.setUnit_price(s.toString());
                    break;
                case 2:
                    item.setReference_name(s.toString());
                    break;
                case 3:
                    item.setEvaluate_source(s.toString());
                    break;
                case 4:
                    item.setContacts(s.toString());
                    break;
            }
        }
    }
}
